using Domain.Entities;
using NHibernate;
using NHibernate.Expression;
using System.Collections.Generic;
using Domain.Misc;

namespace Domain.Services
{
    public static class ForumService
    {
        /*****************************************************************************************
        * Getters
        * ***************************************************************************************/
        public static IList<Forum> GetAll()
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            return session.CreateCriteria(typeof(Forum)).AddOrder(Order.Asc("Index")).List<Forum>();
        }

        public static IList<Forum> GetByCategory(Category category)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            return session.CreateQuery("from Forum f where f.Category.Id = " + category.Id).List<Forum>();
        }

        public static Forum GetById(short forumId)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            return session.Get<Forum>(forumId);
        }

        /*****************************************************************************************
        * Adders
        * ***************************************************************************************/
        public static Forum NewForum(Category category, string name)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                Forum forum = new Forum();
                forum.Category = category;
                forum.Name = name;
                forum.Index = IndexedTools.NextIndex(category.Forums);
                forum.NumberOfReplies = 0;
                forum.NumberOfTopics = 0;
                session.SaveOrUpdate(forum);
                transaction.Commit();
                return forum;
            }
        }

        /*****************************************************************************************
        * Updaters
        * ***************************************************************************************/
        public static void MoveForumUp(Forum forum)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                IList<Forum> forums = forum.Category.Forums;
                if (IndexedTools.MoveUp(forum, forums))
                {
                    foreach (Forum forumToUpdate in forums)
                    {
                        session.SaveOrUpdate(forumToUpdate);
                    }
                }
                transaction.Commit();
            }
        }

        public static void MoveForumDown(Forum forum)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                IList<Forum> forums = forum.Category.Forums;
                if (IndexedTools.MoveDown(forum, forums))
                {
                    foreach (Forum forumToUpdate in forums)
                    {
                        session.SaveOrUpdate(forumToUpdate);
                    }
                }
                transaction.Commit();
            }
        }

        public static void UpdateForum(Forum forum, string name, Category category)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                forum.Name = name;
                forum.Category = category;
                session.SaveOrUpdate(forum);
                transaction.Commit();
            }
        }

        /*****************************************************************************************
        * Deleters
        * ***************************************************************************************/
        public static void RemoveForum(Forum forum)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(forum);
                transaction.Commit();
            }
        }
    }
}
